@echo off

title Is Visual Studio running?
tasklist | findstr /i devenv.exe
IF %ERRORLEVEL% EQU 0 (
cls
@echo If DesignPatterns solution is opened, it's recommended to close Visual Studio while building
pause
)
title building everything
set "strBuilt=Built:"
set "strSkipped=Skipped:"
set "strPacked=Packed:"
set "strFailed=Failed:"
set "strBuiltTemplates=Built Templates:"
set "strFailedTemplates=Failed Templates:"
set "started=%time%"
set "crtdir=%cd%"
cls

title Pre clean and building TemplateLibrary.zip
del /f /q .\..\DesignPatternsAllSetup*.zip
del /f /q /s /AH .\..\Thumbs.db
rmdir /s /q .\..\DesignPatternsSetups
mkdir .\..\DesignPatternsSetups
zipdir.exe "..\DesignPatternsSetups\TemplateLibrary.zip" ".\TemplateLibrary"
IF %ERRORLEVEL% NEQ 0 (
@echo Failed tu build ..\DesignPatternsSetups\TemplateLibrary.zip, level = %ERRORLEVEL%
set strFailedTemplates=%strFailedTemplates% TemplateLibrary.zip
) else (
set strBuiltTemplates=%strBuiltTemplates% TemplateLibrary.zip
)
attrib -r ".\TemplateLibrary.zip"
copy /y "..\DesignPatternsSetups\TemplateLibrary.zip" ".\TemplateLibrary.zip"


rem---------------------------------------------------------

@echo building VS 2015 extension
del /f /q ..\DesignPatternsSetup_v14\*.vsix
IF DEFINED PROGRAMFILES(x86) (
REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\14.0\Setup\VS /v environmentpath
) else (
REG QUERY HKLM\SOFTWARE\Microsoft\VisualStudio\14.0\Setup\VS /v environmentpath
)

IF %ERRORLEVEL% NEQ 0 (
@echo VisualStudio 14.0 is not installed, err level = %ERRORLEVEL%
set strSkipped=%strSkipped% VS2015Bin
pause
) else (
%comspec% /c "VSBuild2.bat 2015 14 .\..\ designPatterns"

IF %ERRORLEVEL% EQU 0 (
set strBuilt=%strBuilt% VS2015Bin
)else (
@echo Failed to build VisualStudio 2015 extension err level = %ERRORLEVEL%
set strFailed=%strFailed% VS2015Bin
set strSkipped=%strSkipped% VS2015Pack
)
)
rem pause
rem-----------------------------------------------------------

IF DEFINED PROGRAMFILES(x86) (
REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\12.0\Setup\VS /v environmentpath
) else (
REG QUERY HKLM\SOFTWARE\Microsoft\VisualStudio\12.0\Setup\VS /v environmentpath
)

IF %ERRORLEVEL% NEQ 0 (
@echo VisualStudio 12.0 is not installed, err level = %ERRORLEVEL%
set strSkipped=%strSkipped% VS2013Bin
) else (
rem %comspec% /c "vsBuild.bat 2013 12 .\..\ VSEmulator"
call vsBuild.bat 2013 12 .\..\ VSEmulator
@echo err level = %ERRORLEVEL%

cd %crtdir%
IF %ERRORLEVEL% EQU 0 (
set strBuilt=%strBuilt% VS2013Bin
rem %comspec% /c "buildSetup.bat 2013 12 .\..\ designPatternsSetup"
call buildSetup.bat 2013 12 .\..\ designPatternsSetup
IF %ERRORLEVEL% EQU 0 (
set strPacked=%strPacked% VS2013Pack
)else (
@echo Failed to pack VisualStudio 2013 add-in err level = %ERRORLEVEL%
set strFailed=%strFailed% VS2013Pack
pause
)
)else (
@echo Failed to build VisualStudio 2013 add-in err level = %ERRORLEVEL%
set strFailed=%strFailed% VS2013Bin
set strSkipped=%strSkipped% VS2013Pack
pause
)
)

cd %crtdir%

IF DEFINED PROGRAMFILES(x86) (
REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\11.0\Setup\VS /v environmentpath
) else (
REG QUERY HKLM\SOFTWARE\Microsoft\VisualStudio\11.0\Setup\VS /v environmentpath
)
IF %ERRORLEVEL% NEQ 0 (
@echo VisualStudio 11.0 is not installed, err level = %ERRORLEVEL%
set strSkipped=%strSkipped% VS2012Bin
) else (
rem %comspec% /c "vsBuild.bat 2012 11 .\..\ VSEmulator"
call vsBuild.bat 2012 11 .\..\ VSEmulator
IF %ERRORLEVEL% EQU 0 (
set strBuilt=%strBuilt% VS2012Bin
rem %comspec% /c "buildSetup.bat 2012 11 .\..\ designPatternsSetup"
call buildSetup.bat 2012 11 .\..\ designPatternsSetup

IF %ERRORLEVEL% EQU 0 (
set strPacked=%strPacked% VS2012Pack
)else (
@echo Failed to pack VisualStudio 2012 add-in err level = %ERRORLEVEL%
set strFailed=%strFailed% VS2012Pack
)
)else (
@echo Failed to build VisualStudio 2012 add-in err level = %ERRORLEVEL%
set strFailed=%strFailed% VS2012Bin
set strSkipped=%strSkipped% VS2012Pack
)
)

cd %crtdir%

IF DEFINED PROGRAMFILES(x86) (
REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\Setup\VS /v environmentpath
) else (
REG QUERY HKLM\SOFTWARE\Microsoft\VisualStudio\10.0\Setup\VS /v environmentpath
)

IF %ERRORLEVEL% NEQ 0 (
@echo VisualStudio 10.0 is not installed, err level = %ERRORLEVEL%
set strSkipped=%strSkipped% VS2010Bin
) else (
rem %comspec% /c "vsBuild.bat 2010 10 .\..\ designPatternsSetup"
call vsBuild.bat 2010 10 .\..\ designPatternsSetup
IF %ERRORLEVEL% EQU 0 (
set strBuilt=%strBuilt% VS2010Bin
set strPacked=%strPacked% VS2010Pack
)else (
@echo Failed to build VisualStudio 2010 add-in err level = %ERRORLEVEL%
set strFailed=%strFailed% VS2010All
)
)

cd %crtdir%

IF DEFINED PROGRAMFILES(x86) (
REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VS /v environmentpath
) else (
REG QUERY HKLM\SOFTWARE\Microsoft\VisualStudio\9.0\Setup\VS /v environmentpath
)

IF %ERRORLEVEL% NEQ 0 (
@echo VisualStudio 9.0 is not installed, err level = %ERRORLEVEL%
set strSkipped=%strSkipped% VS2008Bin
) else (
rem %comspec% /c "vsBuild.bat 2008 9 .\..\ designPatternsSetup"
call vsBuild.bat 2008 9 .\..\ designPatternsSetup

IF %ERRORLEVEL% EQU 0 (
set strBuilt=%strBuilt% VS2008Bin
set strPacked=%strPacked% VS2008Pack
)else (
@echo Failed to build VisualStudio 2008 add-in err level = %ERRORLEVEL%
set strFailed=%strFailed% VS2008All
)
)

cd %crtdir%

SET ThisVersion=2.10
title zipping everything for version %ThisVersion%
xcopy /y /i /f Readme.txt .\..\DesignPatternsSetups
xcopy /y /i /f install.bat .\..\DesignPatternsSetups
zipdir ..\DesignPatternsAllSetups_%ThisVersion%.zip .\..\DesignPatternsSetups

rem xcopy /y /i /f .\..\DesignPatternsSetups\TemplateLibrary.zip .\TemplateLibrary.zip

title zipping packages individually
call makeIndividualZip.bat 2008 %ThisVersion%
call makeIndividualZip.bat 2010 %ThisVersion%
call makeIndividualZip.bat 2012 %ThisVersion%
call makeIndividualZip.bat 2013 %ThisVersion%

title cleaning up
rmdir /s /q ..\DesignPatternsSetup_v9\Release
rmdir /s /q ..\DesignPatternsSetup_v9\Debug
del /f /q ..\DesignPatternsSetup_v9\*.zip
rmdir /s /q ..\DesignPatternsSetup_v10\Release
rmdir /s /q ..\DesignPatternsSetup_v10\Debug
del /f /q ..\DesignPatternsSetup_v10\*.zip
rmdir /s /q ..\DesignPatternsSetup_v11\Release
rmdir /s /q ..\DesignPatternsSetup_v11\Debug
del /f /q ..\DesignPatternsSetup_v11\*.exe
del /f /q ..\DesignPatternsSetup_v11\*.dll
del /f /q ..\DesignPatternsSetup_v11\*.Addin
del /f /q ..\DesignPatternsSetup_v11\*.zip
rmdir /s /q ..\DesignPatternsSetup_v12\Release
rmdir /s /q ..\DesignPatternsSetup_v12\Debug
del /f /q ..\DesignPatternsSetup_v12\*.exe
del /f /q ..\DesignPatternsSetup_v12\*.dll
del /f /q ..\DesignPatternsSetup_v12\*.Addin
del /f /q ..\DesignPatternsSetup_v12\*.zip
del /f /q ..\DesignPatternsSetup_v14\*.vsix
rmdir /s /q ..\DesignPatterns\obj
rmdir /s /q ..\DesignPatterns\bin
rmdir /s /q ..\DesignPatternsWizard\obj
rmdir /s /q ..\DesignPatternsWizard\bin
rmdir /s /q ..\FormTest\obj
rmdir /s /q ..\FormTest\bin
rmdir /s /q ..\Common\obj
rmdir /s /q ..\Common\bin


attrib -h ..\DesignPatterns*.suo /s
del /f /q ..\DesignPatterns*.suo
del /f /q /s .\..\DesignPatternsSetupVS20??.zip

cls
title summary
@echo %strBuilt%
@echo %strSkipped%
@echo %strFailed%
@echo %strPacked%
@echo %strBuiltTemplates%
@echo %strFailedTemplates%
@echo version %ThisVersion% was started at %started% and finished at %time%
rem pause

del /q "..\DotNetZip*.tmp"
del /q "..\Source.zip"
del /q "..\allSource.zip"
del /f /q .\TemplateLibrary.zip
del /f /q .\PremiumTemplateLibrary.zip
rmdir /s /q .\..\DesignPatternsSetups
zipdir.exe "..\Source.zip" "..\..\Designpatterns"

exit /b 0
exit /b %ERRORLEVEL%
